<template>
  <div>
    <AccidentOrder
      v-show="sourceFrom==='1'"
      ref="accidentOrder"
      :value="value"
      :service-level-options.sync="serviceLevelOptions"
      :fault-type-options.sync="faultTypeOptions"
      :supplier-options.sync="supplierOptions"
      :contract-options.sync="contractOptions"
      :is-view="true"
    />
    <TaskOrder
      v-show="sourceFrom==='2'"
      ref="taskOrder"
      :value="value"
      :service-level-options.sync="serviceLevelOptions"
      :fault-type-options.sync="faultTypeOptions"
      :supplier-options.sync="supplierOptions"
      :contract-options.sync="contractOptions"
      :is-view="true"
    />
    <PlanOrder
      v-show="sourceFrom==='3'"
      ref="planOrder"
      :value="value"
      :service-level-options.sync="serviceLevelOptions"
      :fault-type-options.sync="faultTypeOptions"
      :supplier-options.sync="supplierOptions"
    />
    <div style="text-align: right">
      <el-button type="primary" @click="cancel">返回</el-button>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
import { mapGetters } from 'vuex'
import { del } from '@/api/picture'
import { getToken } from '@/utils/auth'
import { get } from '@/api/dictDetail'
import AccidentOrder from './accidentOrder'
import PlanOrder from './planOrder'
import TaskOrder from './taskOrder'
export default {
  name: 'OrderInfo',
  components: { AccidentOrder, PlanOrder, TaskOrder },
  props: {
    value: {
      type: Object,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    },
    sourceFrom: {
      type: String,
      required: true,
      default: '1'
    },
    supThis: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      faultDevices: [],
      devices: [],
      serviceLevelOptions: [],
      faultTypeOptions: [],
      supplierOptions: [],
      contractOptions: [],
      dialogImageUrl: '',
      dialogVisible: false,
      headers: {
        'Authorization': 'Bearer ' + getToken()
      }
    }
  },
  computed: {
    ...mapGetters([
      'imagesUploadApi'
    ])
  },
  mounted() {
    // 加载数据字典
    this.getDict('fault_type')
    this.getDict('service_level')
    this.getSupplier()
    this.getSupplierContract(this.value.supplier)
  },
  methods: {
    getDict(name) {
      return new Promise((resolve, reject) => {
        get(name).then(res => {
          if (name === 'fault_type') {
            const content = [].concat(res.content)
            const _this = this
            content.forEach(function(data, index) {
              const faultTypeOption = { id: data.id, name: data.label }
              _this.faultTypeOptions.push(faultTypeOption)
            })
          } else {
            const content = [].concat(res.content)
            const _this = this
            content.forEach(function(data, index) {
              const serviceLevelOption = { id: data.id, name: data.label }
              _this.serviceLevelOptions.push(serviceLevelOption)
            })
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getSupplier() {
      request({
        url: 'api/tServiceproviderInfoAll',
        method: 'get'
      }).then(res => {
        const content = [].concat(res)
        const _this = this
        content.forEach(function(data, index) {
          const supplierOptions = { id: data.id, name: data.seproRefeName }
          _this.supplierOptions.push(supplierOptions)
        })
      }).catch(err => {
        console.log(err)
      })
    },
    getSupplierContract(id) {
      request({
        url: 'api/tServiceproviderContract?page=0&size=1000&sort=creatTime,desc&serviceproviderId=' + id,
        method: 'get'
      }).then(res => {
        const content = [].concat(res.content)
        const _this = this
        _this.contractOptions = []
        content.forEach(function(data, index) {
          const contractOptions = { id: data.id, name: data.contractName }
          _this.contractOptions.push(contractOptions)
        })
      }).catch(err => {
        console.log(err)
      })
    },
    handleSelect(item) {
      this.getEquipment()
    },
    getEquipment() {
      const params = {
        customerId: this.form.customerId,
        pageNo: 0,
        pageSize: 9999
      }
      request({
        url: 'api/tEquipment',
        method: 'get',
        params
      }).then(res => {
        this.devices = [].concat(res.content)
      }).catch(err => {
        console.log(err)
      })
    },
    handlePrev() {
      this.$emit('prevStep')
    },
    /* cancel() {
      this.resetForm()
    }, */
    handleSubmit() {
      this.$emit('doSubmit')
    },
    cancel() {
      this.$emit('cancel')
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handlePreview(file) {
      window.open(file.url, '_blank')
    },
    handleSuccess(response, file, fileList) {
      const uid = file.uid
      const id = response.data[0]
      this.files.push({ uid, id })
    },
    handleBeforeRemove(file, fileList) {
      const _this = this
      for (let i = 0; i < this.files.length; i++) {
        if (this.files[i].uid === file.uid) {
          del(this.files[i].id).then(res => {
            _this.pictures.splice(i, 1)
          })
          return true
        }
      }
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 2500
      })
    }
  }
}
</script>

<style scoped>

</style>
